<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
ajaxUtil
    <input type="button" value="提交" onclick="save()" />
</body>
<!-- 根路径 -->
<script type="text/javascript" th:inline="javascript">
    let ctxPath = /*[[@{/}]]*/'';
</script>
<script th:src="@{/js/ctx.js}"></script>
<script th:src="@{/js/jquery-2.1.4.min.js}"></script>
<script>
const fetchUtil = {
    doPost:function (url,data) {
        return fetch(url, {
            method: 'POST', // or 'PUT'
            body: JSON.stringify(data), // data can be `string` or {object}!
            headers: new Headers({
                'Content-Type': 'application/json'
            })
        }).then(res => res.json())
    },
    // postData('http://example.com/answer', {answer: 42})
    // .then(data => console.log(data))
    // .catch(error => console.error(error))
    doPost1:function(url,data){
        return fetch(url, {
            body: JSON.stringify(data), // must match 'Content-Type' header
            cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
            credentials: 'same-origin', // include, same-origin, *omit
            headers: {
                'user-agent': 'Mozilla/4.0 MDN Example',
                'content-type': 'application/json'
            },
            method: 'POST', // *GET, POST, PUT, DELETE, etc.
            mode: 'cors', // no-cors, cors, *same-origin
            redirect: 'follow', // manual, *follow, error
            referrer: 'no-referrer', // *client, no-referrer
        }).then(response => response.json()) // parses response to JSON
    },
    doUpload:function (url,inputFileId) {
        var formData = new FormData();
        var fileField = document.querySelector("#"+inputFileId);
        formData.append('username', 'abc123');
        formData.append('avatar', fileField.files[0]);
        fetch(url, {
            method: 'PUT',
            body: formData
        })
            .then(response => response.json())
            .catch(error => console.error('Error:', error))
            .then(response => console.log('Success:', response));
    }
}
</script>
<script>
    function save() {
        let url = ctxPath + 'mybatis/commonMgr';
        let data = {
            id:1,
            name:'ljw'
        };
        fetchUtil.doPost(url,data)
            .then(data => console.log("success:",data))
            .catch(error => console.error("error:",error))
    }
</script>
</html>